package com.ruoyi.custom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.custom.domain.Custom;
import com.ruoyi.custom.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.CustomRecordMapper;
import com.ruoyi.custom.domain.CustomRecord;
import com.ruoyi.custom.service.ICustomRecordService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;
import static org.apache.commons.lang3.SystemUtils.getUserName;

/**
 * 客户跟进Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@Service
public class CustomRecordServiceImpl extends ServiceImpl<CustomRecordMapper, CustomRecord> implements ICustomRecordService
{
    @Autowired
    private CustomRecordMapper customRecordMapper;
    @Autowired
    private ICustomService customService;

    /**
     * 查询客户跟进
     *
     * @param id 客户跟进主键
     * @return 客户跟进
     */
    @Override
    public CustomRecord selectCustomRecordById(Long id)
    {
        return customRecordMapper.selectCustomRecordById(id);
    }

    public CustomRecord selectLastRecord(Long customId){
        return customRecordMapper.selectLastRecord(customId);
    }

    /**
     * 查询客户跟进列表
     *
     * @param customRecord 客户跟进
     * @return 客户跟进
     */
    @Override
    public List<CustomRecord> selectCustomRecordList(CustomRecord customRecord)
    {
        return customRecordMapper.selectCustomRecordList(customRecord);
    }

    /**
     * 新增客户跟进
     *
     * @param customRecord 客户跟进
     * @return 结果
     */
    @Override
    public int insertCustomRecord(CustomRecord customRecord)
    {
       Custom custom = customService.selectCustomById(customRecord.getCustomId());
        customRecord.setCreateBy(custom!=null?custom.getUserName():null);
        customRecord.setCreateById(custom!=null?custom.getUserId():null);
        customRecord.setCreateTime(DateUtils.getNowDate());
        return customRecordMapper.insertCustomRecord(customRecord);
    }

    /**
     * 修改客户跟进
     *
     * @param customRecord 客户跟进
     * @return 结果
     */
    @Override
    public int updateCustomRecord(CustomRecord customRecord)
    {
        return customRecordMapper.updateCustomRecord(customRecord);
    }

    /**
     * 批量删除客户跟进
     *
     * @param ids 需要删除的客户跟进主键
     * @return 结果
     */
    @Override
    public int deleteCustomRecordByIds(Long[] ids)
    {
        return customRecordMapper.deleteCustomRecordByIds(ids);
    }

    /**
     * 删除客户跟进信息
     *
     * @param id 客户跟进主键
     * @return 结果
     */
    @Override
    public int deleteCustomRecordById(Long id)
    {
        return customRecordMapper.deleteCustomRecordById(id);
    }
}
